package com.feibi.study.View

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.feibi.study.R

class UnderLineTextView : ConstraintLayout {

    private lateinit var mUnderline: View
    private lateinit var mTextView: TextView

    var text = ""
        set(value) {
            field = value
            mTextView.text = text
        }

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        LayoutInflater.from(context).inflate(R.layout.underline_textview, this)
        initViews(this)
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnderLineTextView)
        mTextView.text = typedArray.getString(R.styleable.UnderLineTextView_utv_text)
        val defTextSize = resources.getDimensionPixelSize(R.dimen.text_size)
        mTextView.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            typedArray.getDimensionPixelSize(R.styleable.UnderLineTextView_utv_textSize, defTextSize).toFloat()
        )
        val defColor = resources.getColor(R.color.green)
        mTextView.setTextColor(typedArray.getColor(R.styleable.UnderLineTextView_utv_textColor, defColor))
        mUnderline.setBackgroundColor(typedArray.getColor(R.styleable.UnderLineTextView_utv_textColor, defColor))
        val defValue = resources.getDimensionPixelSize(R.dimen.underline_height)
        val height = typedArray.getDimensionPixelSize(R.styleable.UnderLineTextView_utv_lineHeight, defValue)
        val iconLp: ViewGroup.LayoutParams = mUnderline.layoutParams
        iconLp.height = height
        mUnderline.layoutParams = iconLp
        typedArray.recycle()
    }

    private fun initViews(view: View) {
        mTextView = view.findViewById(R.id.textView)
        mUnderline = view.findViewById(R.id.underline)
    }

}